<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="批准文号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'yid', validatorRules.yid]" placeholder="请输入批准文号"></a-input>
        </a-form-item>
        <a-form-item label="药品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'yname', validatorRules.yname]" placeholder="请输入药品名称"></a-input>
        </a-form-item>
        <a-form-item label="药品图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-upload
            listType="picture-card"
            class="avatar-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1}"
            :headers="headers"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="picUrl" :src="getAvatarView()" alt="图片" style="height:104px;max-width:300px"/>
            <div v-else>
              <a-icon :type="uploadLoading ? 'loading' : 'plus'"/>
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item label="药品类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag v-decorator="[ 'ylid', validatorRules.ylid]" :triggerChange="true" placeholder="请选择药品类别"
                             dictCode="medicine_cate,ylname,ylid">
          </j-dict-select-tag>
        </a-form-item>
        <a-form-item label="参考进价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'ypprice', validatorRules.ypprice]" placeholder="请输入参考进价" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="参考售价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'ysprice', validatorRules.ysprice]" placeholder="请输入参考售价" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'yspecifications', validatorRules.yspecifications]" placeholder="请输入规格"></a-input>
        </a-form-item>
        <a-form-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'gysname', validatorRules.gysname]" placeholder="请输入供应商名称"></a-input>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'yremarks', validatorRules.yremarks]" placeholder="请输入备注"></a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'
    import {axios} from '@/utils/request'
    import Vue from 'vue'
    import {ACCESS_TOKEN} from "@/store/mutation-types"

    export default {
        name: "MedicineInfoModal",
        components: {
            JDate,
        },
        data() {
            return {
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },
                confirmLoading: false,
                validatorRules: {
                    yid: {rules: [{required: true, message: '请输入药品编号!'}]},
                    yname: {rules: [{required: true, message: '请选择药品名称!'}]},
                    ylid: {rules: [{required: true, message: '请输入药品类别!'}]},
                    ypprice: {rules: [{required: true, message: '请输入参考进价!'}]},
                    ysprice: {rules: [{required: true, message: '请输入参考售价!'}]},
                    yspecifications: {rules: [{required: true, message: '请输入规格!'}]},
                    yremarks: {},
                    gysname: {rules: [{required: true, message: '请输入供应商名称!'}]},
                },
                url: {
                    add: "/medicine/medicineInfo/add",
                    edit: "/medicine/medicineInfo/edit",
                    fileUpload: window._CONFIG['domianURL'] + "/sys/common/upload",//图片上传请求
                    imgerver: window._CONFIG['domianURL'] + "/sys/common/view",//图片回显请求
                },
                data: [],
                headers:{},
                uploadLoading:false,
                picUrl: "",
            }
        },
        created() {
            const token = Vue.ls.get(ACCESS_TOKEN);
            this.headers = {"X-Access-Token":token}
        },
        computed:{
            uploadAction:function () {
                return this.url.fileUpload;
            }
        },
        methods: {
            add() {
                this.picUrl = "";
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                if(record.hasOwnProperty("id")){
                    this.picUrl = "Has no pic url yet";
                }
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'yid', 'yname', 'ylid', 'ypprice', 'ysprice', 'yspecifications', 'yremarks', 'gysname'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let avatar = that.model.avatar;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        formData.avatar = avatar;
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'yid', 'yname', 'ylid', 'ypprice', 'ysprice', 'yspecifications', 'yremarks', 'gysname'))
            },
            beforeUpload: function(file){
                var fileType = file.type;
                if(fileType.indexOf('image')<0){
                    this.$message.warning('请上传图片');
                    return false;
                }
                //TODO 验证文件大小
            },
            handleChange (info) {
                this.picUrl = "";
                if (info.file.status === 'uploading') {
                    this.uploadLoading = true;
                    return
                }
                if (info.file.status === 'done') {
                    var response = info.file.response;
                    this.uploadLoading = false;
                    console.log(response);
                    if(response.success){
                        this.model.avatar = response.message;
                        this.picUrl = "Has no pic url yet";
                    }else{
                        this.$message.warning(response.message);
                    }
                }
            },
            getAvatarView(){
                return this.url.imgerver +"/"+ this.model.avatar;
            },
        }
    }
</script>